﻿using Comm.Model;
using Comm.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Comm.Web.View
{
    public partial class UserManageCenter : Basepage
    {
        StringBuilder sb = new StringBuilder();
        protected string userInfoHtml = string.Empty;
        BLL.UsersService blluser = new BLL.UsersService();
        BLL.OrdersService bllorders = new BLL.OrdersService ();
        BLL.CheckAddressService blladdress = new BLL.CheckAddressService();
        BLL.MBQuestionService bllmbquestion = new BLL.MBQuestionService();
        BLL.MiBaoService bllmibao = new BLL.MiBaoService();
        public override void SonLoad()
        {
            if (!IsPostBack)
            {
                UserList();
            }
            if (Request.IsPostBack())
            {
                foreach (string key in Request.Files)
                {
                    string Directoryfile = UserNow.uLoginName + UserNow.uId;
                    string strNewImgName = Request.Files[key].FileName;
                    string _path = Server.MapPath("~/Upload/" + Directoryfile);
                    if (Directory.Exists(_path))
                    {
                        Directory.Delete(_path, true);
                    }
                    Directory.CreateDirectory(Server.MapPath("~/Upload/" + Directoryfile));
                    Request.Files[key].SaveAs(Server.MapPath("~/Upload/" + Directoryfile + "/" + strNewImgName));
                    Response.Write("../Upload/" + Directoryfile + "/" + strNewImgName);
                }
                Response.End();
            }
        }
        public void UserList() 
        {
            int usertype = 0;
            if (!int.TryParse(Request.QueryString["userList"], out usertype))
            {
                usertype = 0;
            }
            if (usertype == 0) 
            {
               sb.Append("<div class=\"meb-content\">");
               sb.Append("<div class=\"meb-avatar pull-left\">");           
               string imgName = UserNow.uLoginName + UserNow.uId;
               string path = Server.MapPath("~/Upload/" + imgName);
               if (Directory.Exists(path)) 
               {
                   string[] fn = Directory.GetFiles(path);
                   DirectoryInfo dinfo = new DirectoryInfo(path);
                   FileInfo[] finfo = null;
                   if (dinfo.Exists)
                   {
                       finfo = dinfo.GetFiles();
                   }
                   if (File.Exists(fn[0]))
                   {
                       sb.Append("<img id=\"img\" alt=\"\" src=\"../Upload/" + imgName + "/" + finfo[0] + "\" />");
                   }
               }
               else
               {
                   sb.Append("<img id=\"img\" alt=\"\" src=\"../images/avatar.png\" />");   
               }
               sb.Append("<div id=\"swfu_container\" style=\"margin: 10px 25px;\">");
               sb.Append("<div><span id=\"spanButtonPlaceholder\"></span></div><div id=\"divFileProgressContainer\" style=\"height: 75px;\"></div></div>");
               sb.Append("</div><div class=\"meb-infor pull-right\"><div class=\"meb-infor-title mb10\"><div>"+UserNow.uLoginName+"</div>欢迎您!</div>");
               sb.Append("<div class=\"account-security mb10\">账户安全：<b class=\"orange-font\">中级</b> <i class=\"icon-main icon-security-4\"></i>");
               sb.Append("<i class=\"icon-main icon-phone-bright\"></i>手机已验证 ");
               sb.Append("<i class=\"icon-main icon-email-bright\"></i>邮箱已验证 ");
               sb.Append("<i class=\"icon-main icon-regular-bright\"></i>正常</div>");
               sb.Append("<div class=\"information\"><ul><li class=\"information-tit\">优惠宝：</li>");
               sb.Append("<li class=\"information-show\"><span class=\"pull-right\"><a href=\"\">什么是优惠宝？</a></span><a href=\"\">1000(=10元)</a></li>");
               sb.Append("<hr><li class=\"information-tit\">已完成：</li><li class=\"information-show\"><a href=\"\">全部订单(5)</a></li>");
               sb.Append("<hr><li class=\"information-tit\">待处理：</li><li class=\"information-show\"> <a href=\"\">待付款(4)</a> &nbsp;<a href=\"\">待发货(0)</a> &nbsp;");
               sb.Append("<a href=\"\">待收货(0)</a> &nbsp;<a href=\"\">待评价(0)</a> &nbsp;<a href=\"\">待晒单(0)</a></li>");
               sb.Append("<hr><li class=\"information-tit\">总积分：</li><li class=\"information-show\"><span class=\"pull-right\"><a href=\"\">积分规则</a></span><a href=\"\">0</a></li>");
               sb.Append("<hr><li class=\"information-tit last-li\">会员等级：</li><li class=\"information-show last-li\">");
               sb.Append("<span class=\"pull-right\">亲！您今天还没有签到哦！签到可获得<b class=\"red-font f14\">10</b>优惠宝");
               sb.Append("<button type=\"button\" class=\"btn btn-warning btn-xs\">我要签到</button></span><a href=\"\">49</a>&nbsp;<i class=\"icon-main icon-level-1\"></i></li></ul></div></div>");
               sb.Append("<div class=\"clearfix\"></div></div>");
            }
            else if (usertype == 1)
            {
                Model.Users model = blluser.QuerySingle(UserNow.uId);
                sb.Append("<iframe name=\"myiframe\" width=\"0\" height=\"0\" scrolling=\"no\"></iframe>");
                sb.Append("<form id=\"fSInfo\" target=\"myiframe\"><div style=\"text-align:center; margin-top:30px;\">");
                sb.Append("<h2 style=\"font-size:16px; text-align:center;\">用户" + UserNow.uLoginName + "的个人详细信息:</h2>");
                sb.Append("<div style=\"margin-top:50px;\">");
                sb.Append("<div><span>姓名：</span><input type=\"text\" id=\"txtName\" name=\"txtName\" style=\"width:300px;\" disabled=\"true\" size=\"50\" value=\"" + UserNow.uLoginName + "\" /><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div>");
                sb.Append("<div><span>别名：</span><input type=\"text\" id=\"txtotherName\" name=\"txtotherName\" style=\"width:300px;\" size=\"50\" value=\"" + model.otherName + "\" /><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div>");
                sb.Append("<div><span>地址：</span><input type=\"text\" id=\"txtAddress\" name=\"txtAddress\" style=\"width:300px;\" size=\"50\"  value=\"" + model.Address + "\" /><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div>");
                sb.Append("<div><span>电话：</span><input type=\"text\" id=\"txtPhone\" name=\"txtPhone\" style=\"width:300px;\" size=\"50\"  value=\"" + model.Phone + "\" /><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div>");
                sb.Append("<div><span>邮箱：</span><input type=\"text\" id=\"txtEmail\" name=\"txtEmail\" style=\"width:300px;\" size=\"50\"  value=\"" + model.Mail + "\" /><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div></div>");
                sb.Append("<input class=\"button button-primary button-rounded button-small\" style=\"background-color:#524F48;width:187px;margin-top:10px; height: 25px; margin-left:60px;line-height: 25px; border: 0px;\" type=\"submit\" id=\"btnSuInfo\" value=\"确定完善信息\"/>");
                sb.Append("</div></form>");
            }
            else if (usertype == 2)
            {
                sb.Append("<div style=\"text-align:center; margin-top:30px;\">");
                sb.Append("<h2 style=\"font-size:16px; text-align:center;\">用户" + UserNow.uLoginName + "在修改密码</h2>");
                sb.Append("<div style=\"margin-top:50px;\">");
                sb.Append("<div><span style=\"margin-right:23px;\">姓    名：</span><input type=\"text\" id=\"txtName\" name=\"txtName\" style=\"width:300px;\" size=\"50\" value=\"" + UserNow.uLoginName + "\" /><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div>");
                sb.Append("<div><span>原始密码：</span><input type=\"text\" name=\"txtPwd\" id=\"txtPwd\" style=\"width:300px;\" size=\"50\"  /><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div></div>");
                sb.Append("<input class=\"button button-primary button-rounded button-small\" style=\"background-color:#524F48;width:187px;margin-top:10px; height: 25px; margin-left:60px;line-height: 25px; border: 0px;\" type=\"submit\" id=\"btnYanZ\" value=\"验    证\"/>");
                sb.Append("</div>");
            }
            else if (usertype == 3)
            {
                if (UserNow.uLoginName == "admin")
                {
                    sb.Append("<iframe name=\"myiframe\" width=\"0\" height=\"0\"></iframe><form id=\"form1\" style=\"margin:20px;\" runat=\"server\" action=\"../Action/UserManageCenter.ashx\" target=\"myiframe\" method=\"post\" enctype=\"multipart/form-data\"><div style=\"\">");
                    sb.Append("<input type=\"hidden\" name=\"type\" id=\"type\" value=\"txt\"/>");
                    sb.Append("<input class=\"easyui-filebox\" data-options=\"prompt:'选择文件...'\" name=\"file\" id=\"file\" style=\"width:400px;\" />");
                    sb.Append("<input class=\"button button-primary button-rounded button-small\" style=\"background-color:#524F48;width:60px; height: 25px; margin-left:10px;line-height: 25px; border: 0px;\" type=\"submit\" name=\"btnAddWord\" id=\"btnAddWord\" value=\"添加\"/>");
                    sb.Append("<input class=\"button button-primary button-rounded button-small\" style=\"background-color:#524F48;width:60px;height: 25px;text-align:center; margin-left:10px;line-height: 25px; border: 0px;\" type=\"submit\" name=\"btnLook\" id=\"btnLook\" value=\"查看\"/></div>");
                    sb.Append("<div style=\"position: relative; \">");
                    sb.Append("<textarea id=\"txtContent\" name=\"txtContent\" rows=\"20\" style=\"width:auto;height:auto;background-color:#6D6666;\" cols=\"60\"></textarea>");
                    sb.Append("<input class=\"button button-primary button-rounded button-small\" style=\"background-color:#524F48;width:60px;position: absolute; cursor:pointer; bottom:10px; height: 25px; margin-left:5px;line-height: 25px; border: 0px;\" type=\"submit\" name=\"btnDownWord\" id=\"btnDownWord\" value=\"下载\"/></div></form>");
                }
                else 
                {
                    IList<CheckAddress> list = blladdress.GetAddressList(UserNow.uId);
                    if (list != null)
                    {
                        sb.Append("<table id=\"tbList\" style=\"width:90%;\">");
                        sb.Append("<tr id=\"trNav\"><th colspan=\"5\">用户【" + UserNow.uLoginName + "】的收货地址</th></tr>");
                        sb.Append("<tr><th>收货人</th><th>地址</th><th>电话</th><th>邮编</th><th>操作</th></tr>");
                        foreach (CheckAddress model in list)
                        {
                            sb.Append("<tr>");
                            sb.Append("<td style=\"width:15%\">" + model.UserName + "</td>");
                            sb.Append("<td style=\"width:35%\">" + model.Address + "</td>");
                            sb.Append("<td style=\"width:25%\">" + model.TelPhone + "</td>");
                            sb.Append("<td style=\"width:10%\">" + model.postCode + "</td>");
                            sb.Append("<td style=\"width:15%\"><a href=\"javascript:void(0)\" onclick=\"doAddressDel(" + model.Id + ",this)\" style=\"cursor:pointer;\">删除</a></td></tr>");
                        }
                        sb.Append("</table>");
                    }
                }
            }
            else if (usertype == 4)
            {
                IList<Orders> model = bllorders.GetOrderStatuIs0(UserNow.uId);
                if (model != null)
                {
                    if (model.Count > 0)
                    {
                        int totalScore = 0;
                        foreach (Orders order in model)
                        {
                            totalScore += Convert.ToInt32(order.TotalPrice);
                        }
                        sb.Append("<iframe name=\"myiframe1\" width=\"0\" height=\"0\" scrolling=\"no\"></iframe>");
                        sb.Append("<div style=\"margin-top:50px; text-align:center;\">");
                        sb.Append("<div><span>您的积分为：" + totalScore + "     是否查看<a title=\"详细积分\" style=\"cursor:pointer;\" target=\"myiframe1\">详细积分？</a></span></div>");
                        sb.Append("<div style=\"width:100%;\" id=\"scoreList\"></div></div>");
                    }
                }
                else
                {
                    sb.Append("<iframe name=\"myiframe1\" width=\"0\" height=\"0\" scrolling=\"no\"></iframe>");
                    sb.Append("<div style=\"margin-top:50px; text-align:center;\">");
                    sb.Append("<div><span>您暂时没有积分</span></div>");
                    sb.Append("<div style=\"width:100%;\" id=\"scoreList\"></div></div>");
                }
            }
            else if (usertype == 5)
            {
                string optHtml = string.Empty;
                IList<MiBao > mibao = bllmibao.getAllMiBao(UserNow.uId);
                if (mibao == null)
                {
                    IList<MBQuestion> list = bllmbquestion.getMBquestion();
                    foreach (MBQuestion model in list)
                    {
                        optHtml += "<option value=" + model.Id + ">" + model.QuestionM + "</option>";
                    }
                    sb.Append("<iframe name=\"myiframe1\" width=\"0\" height=\"0\" scrolling=\"no\"></iframe>");
                    sb.Append("<form id=\"fSInfoMobao\" target=\"myiframe1\"><div style=\"text-align:center; margin-top:30px;\">");
                    sb.Append("<h2 style=\"font-size:16px; text-align:center;\">用户" + UserNow.uLoginName + "正在填写密保</h2>");
                    sb.Append("<div style=\"margin-top:50px;\">");
                    sb.Append("<div><span>密保问题一：</span><select type=\"text\" id=\"MiBao1\" name=\"MiBao1\" style=\"width:300px;\">" + optHtml + "</select><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\"/></div>");
                    sb.Append("<div><span>密保答案：</span><input type=\"text\" id=\"Question1\" name=\"Question1\" style=\"width:288px; margin-left:12px;\" size=\"50\" /><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div>");
                    sb.Append("<div><span>密保问题二：</span><select type=\"text\" id=\"MiBao2\" name=\"MiBao2\" style=\"width:300px;\">" + optHtml + "</select><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\"/></div>");
                    sb.Append("<div><span>密保答案：</span><input type=\"text\" id=\"Question2\" name=\"Question2\" style=\"width:288px; margin-left:12px;\" size=\"50\" /><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div>");
                    sb.Append("<div><span>密保问题三：</span><select type=\"text\" id=\"MiBao3\" name=\"MiBao3\" style=\"width:300px;\">" + optHtml + "</select><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\"/></div>");
                    sb.Append("<div><span>密保答案：</span><input type=\"text\" id=\"Question3\" name=\"Question3\" style=\"width:288px; margin-left:12px;\" size=\"50\"/><img  src=\"../images/cha.ico\" style=\"display:none\"  width=\"15\" height=\"15\" /></div></div>");
                    sb.Append("<input class=\"button button-primary button-rounded button-small\" style=\"background-color:#524F48;width:187px;margin-top:10px; height: 25px; margin-left:60px;line-height: 25px; border: 0px;\" type=\"submit\" id=\"btnSure\" value=\"确认\"/>");
                    sb.Append("</div></form>");
                }
                else
                {
                    sb.Append("<div style=\"margin-top:50px; text-align:center;\">");
                    sb.Append("<div><span>您已经设置了密保（密保是唯一的，如果忘记了请尽快想管理员<a href=\"#\">申述</a>）</span></div>");
                    sb.Append("</div>");
                }
            }
            userInfoHtml = sb.ToString();
        }
    }
}